package com.baor.service.impl;

import com.baor.dao.LeaveDao;
import com.baor.dao.LeaveTypeDao;
import com.baor.domain.LeaveTypeVo;
import com.baor.domain.LeaveVo;
import com.baor.service.LeaveService;
import com.baor.utils.AddDaysUtil;
import com.baor.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LeaveServiceImpl implements LeaveService {

    int row = 0;
    AjaxResult<?> rs = null;

    @Autowired
    private LeaveDao leaveDao;
    @Autowired
    private LeaveTypeDao leaveTypeDao;

    @Override
    public AjaxResult<?> LeaveList(Map<String, Object> param) {
        List<LeaveVo> relaxVo = leaveDao.LeaveList(param);
        Integer pagecount = leaveDao.pageCount(param);
        Map<String, Object> map = new HashMap<>();
        map.put("leaveList", relaxVo);
        map.put("pageCount", pagecount);
        AjaxResult<?> result = AjaxResult.success(map);
        return result;
    }

    @Override
    public AjaxResult<?> addLeave(Map<String, Object> param) {
        String leaveStartTime = (String) param.get("leaveStarttime");
        Integer leaveDay = null;
        if (param.get("leaveDay") instanceof Integer) {
            leaveDay = (Integer) param.get("leaveDay");
        } else if (param.get("leaveDay") instanceof String) {
            leaveDay = Integer.parseInt((String) param.get("leaveDay"));
        } else {
            // 处理参数异常情况，例如给予默认值或者抛出异常
        }
        String leaveEndtime = AddDaysUtil.addDays(leaveStartTime, leaveDay);
        param.put("leaveEndtime", leaveEndtime);
        int row = leaveDao.addLeave(param);
        if (row > 0) {
            rs = AjaxResult.success("添加成功");
        } else {
            rs = AjaxResult.error("添加失败");
        }
        return rs;
    }

    @Override
    public AjaxResult<?> LeaveInfo(int LeaveId) {
        LeaveVo leaveVo = this.leaveDao.LeaveInfo(LeaveId);
        return AjaxResult.success(leaveVo);
    }

    @Override
    public AjaxResult<?> addLeaveInfo() {
        List<LeaveTypeVo> leaveTypeVos = leaveTypeDao.selectList(null);
        Map<String, Object> type = new HashMap<>();
        type.put("leaveTypeName", leaveTypeVos);
        Map<String, Object> dataset = new HashMap<>();
        dataset.put("type", type);
        dataset.put("param", leaveDao.addLeaveInfo());
        return AjaxResult.success(dataset);
    }

    @Override
    public AjaxResult<?> updateLeaveInfo(Map<String, Object> param) {
        String leaveStartTime = (String) param.get("leaveStartTime");
        Integer leaveDay = null;
        if (param.get("leaveDay") instanceof Integer) {
            leaveDay = (Integer) param.get("leaveDay");
        } else if (param.get("leaveDay") instanceof String) {
            leaveDay = Integer.parseInt((String) param.get("leaveDay"));
        } else {
            // 处理参数异常情况，例如给予默认值或者抛出异常
        }
        String leaveEndtime = AddDaysUtil.addDays(leaveStartTime, leaveDay);
        param.put("leaveEndtime", leaveEndtime);
        row = leaveDao.updateLeaveInfo(param);
        if (row > 0) {
            rs = AjaxResult.success("修改成功");
        } else {
            rs = AjaxResult.error("修改失败");
        }
        return rs;
    }

    @Override
    public AjaxResult<?> deleteLeaveInfo(Map<String, Object> param) {
        param.put("delflag", 1); // 设置 delflag = 1
        int row = leaveDao.updateLeaveInfo(param);  // 调用更新方法
        if (row > 0) {
            rs = AjaxResult.success("删除成功");
        } else {
                rs = AjaxResult.error("删除失败");
        }
        return rs;
    }

}


